﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace Turners.DMS.SearchWebParts.SearchInvoicesWebPart
{
    public partial class SearchInvoicesWebPartUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void SearchButton_Click(object sender, EventArgs e)
        {
            using (SPSite currentSite = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb homeWeb = currentSite.AllWebs["Search"])
                {
                    string searchResultsPageUrl = homeWeb.Url + "/results.aspx?k=";

                    if (SearchAllTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += SearchAllTextBox.Text;
                    }
                    if (DocumentNumberTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " DocumentNumber:\"" + DocumentNumberTextBox.Text + "\"";
                    }
                    if (DateStartTextBox.Text != String.Empty)
                    {
                        string reformattedDate = DateStartTextBox.Text.Split('/')[1] + "/" + DateStartTextBox.Text.Split('/')[0] + "/" + DateStartTextBox.Text.Split('/')[2];
                        searchResultsPageUrl += " InvoiceDate>\"" + reformattedDate + "\"";
                    }
                    if (DateEndTextBox.Text != String.Empty)
                    {
                        string reformattedDate = DateEndTextBox.Text.Split('/')[1] + "/" + DateEndTextBox.Text.Split('/')[0] + "/" + DateEndTextBox.Text.Split('/')[2];
                        searchResultsPageUrl += " InvoiceDate<\"" + reformattedDate + "\"";
                    }
                    if (InvoiceTypeCodeDropDown.Text != String.Empty)
                    {
                        searchResultsPageUrl += " InvoiceType:\"" + InvoiceTypeCodeDropDown.Text + "\"";
                    }
                    if (CustomerNumberTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " CustomerNumber:\"" + CustomerNumberTextBox.Text + "\"";
                    }
                    if (CustomerNameTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " CustomerName:\"" + CustomerNameTextBox.Text + "\"";
                    }
                    if (BranchDropDown.Text != String.Empty)
                    {
                        searchResultsPageUrl += " BranchName:\"" + BranchDropDown.Text + "\"";
                    }
                    if (GoodNumberTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " GoodNumber:\"" + GoodNumberTextBox.Text + "\"";
                    }
                    if (PlateNumberTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " PlateNumber:\"" + PlateNumberTextBox.Text + "\"";
                    }
                    if (ConsignmentVendorNumberTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " ConsignmentVendorNumber:\"" + ConsignmentVendorNumberTextBox.Text + "\"";
                    }
                    if (ConsignmentGoodNumberTextBox.Text != String.Empty)
                    {
                        searchResultsPageUrl += " ConsignmentGoodNumber:\"" + ConsignmentGoodNumberTextBox.Text + "\"";
                    }


                    searchResultsPageUrl += "&r=fileextension%3D%22aspx%22";

                    Response.Redirect(searchResultsPageUrl);
                }

            }

        }
    }
}
